function ViewLoader () {
	
	var _this = this;

	this.grids = {};
	this.selects = {};
	//this.trees = {};
	this.completes ={};
	
	this.initViews = function (){
		
		_this.setActiveMenu();
		_this.createTrees();
		_this.createGrids();
		_this.createSelects();
		_this.createQuerys();
		_this.createDatePicker();
		_this.createAutoComplete();
		
	};
	
	this.createAutoComplete = function(){
		
		for (var i = 0; i < nebula.completes.length; i++) {
			var config = nebula.completes[i];
			var args = config.dataset.split(/\./);
			var catalog = args[0];
			var entity = args[1];
			var entityParam = "?nebula_req_param_catalog="+catalog+"&nebula_req_param_entity="+entity+"&nebula_req_param_compname="+config.name;
	
			$('#'+config.id).typeahead({ 
				ajax: { 
					url: '/NebulaWeb/base/list/autoComplete'+entityParam, 
					displayField:"keyword",
					triggerLength: 1 ,
				}, 
				highlighter: function (item) {
		            var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&');
		            return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
		                return '<strong style="color:blue">' + match + '</strong>';
		            });
		        },
				display:"projectName",
				itemSelected: function (item, val, text) { 
					$('.alert').show().html('You selected <strong>' + val + '</strong>: <strong>' + text + '</strong>'); 
				} 
			});
		}
	};	
	
	this.createDatePicker = function(){
		
		$('.form-date').datetimepicker({
			language:  'zh-CN',
	        weekStart: 1,
	        todayBtn:  1,
			autoclose: 1,
			todayHighlight: 1,
			forceParse: 0,
			format:"yyyy-mm-dd",
			minView: 2,
			startView:2,
			
	    });
		$('.form-date > .form-date-remove').click(function(){
			
			$(this).parent().find("input[type='text']").val("");
			
		});
		
		$('.form-date-month').datetimepicker({
			language:  'zh-CN',
	        weekStart: 1,
	        todayBtn:  1,
			autoclose: 1,
			todayHighlight: 1,
			forceParse: 0,
			format:"yyyy-mm",
			minView: 3,
			startView:3,
			
	    });
		$('.form-date-month > .form-date-remove').click(function(){
			
			$(this).parent().find("input[type='text']").val("");
			
		});
		
	};
	
	this.createQuerys = function(){
		
		for (var i = 0; i < nebula.querys.length; i++) {
			var queryCfg = nebula.querys[i];
			var searchForm = $("#"+queryCfg.id);
			searchForm.submit(function() {
				queryGrid(this,queryCfg.id,queryCfg.target);
				return false;
			});
		}
	};
	
	this.setActiveMenu = function(){
		$("#nav-accordion a").each(function(){
			var path = $(this).attr("href");
			var url = window.location.href;
			if(path != nebula.contextPath && url.indexOf(path) >= 0){
				$(this).parents("ul").css({display: "block"});
				$(this).parents(".sub-menu").find(">a").addClass("active");
				$(this).closest("li").addClass("active");
			}
		});
	};
	
	this.createTrees = function(){
		for (var i = 0; i < nebula.trees.length; i++) {
			var treeCfg = nebula.trees[i];
			if(treeCfg.dataset == undefined || treeCfg.dataset == ""){
				treeCfg.dataset = nebula.catalog + "." + nebula.entity;
			}
			
			new TreeView(treeCfg);
		}
	};
	
	this.createSelects = function(){
		for (var i = 0; i < nebula.selects.length; i++) {
			var selectCfg = nebula.selects[i];
			if(selectCfg.dataset == undefined || selectCfg.dataset == ""){
				selectCfg.dataset = nebula.catalog + "." + nebula.entity;
			}
			_this.selects[selectCfg.id] = new SelectView(selectCfg);
		}
	};
	/*
	this.createAutoCompletes = function(){
		for (var i = 0; i < nebula.completes.length; i++) {
			var completeCfg = nebula.completes[i];
			
			$("#autocomplete2").autocomplete({
			    url:'search.php?type=xml',
			    minchar:2,
			    delay:1000, // in milliseconds
			    onSelect:function(){
			        alert(this.ac.val())
			    },
			    type:'xml'
			});
			
			//if(selectCfg.dataset == undefined || selectCfg.dataset == ""){
				//selectCfg.dataset = nebula.catalog + "." + nebula.entity;
			//}
			//_this.selects[selectCfg.id] = new SelectView(selectCfg);
		}
	};*/
	
	this.createGrids = function(){
		for (var i = 0; i < nebula.grids.length; i++) {
			var gridCfg = nebula.grids[i];
			_this.grids[gridCfg.gridId] = new GridView(gridCfg);
		}
	};
	
};
var ViewLoader = new ViewLoader();